﻿using Common;
using DadayunApi.Model;
using KhtApi.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KhtMaintain
{
    public partial class FrmSettings : Form
    {
        string settingFile = "settings.ini";
        MyConfiguration cfg = new MyConfiguration(true);
        public FrmSettings()
        {

            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
           
            try
            {
                AppAuthBase authBase = new AppAuthBase
                {
                    AppKey = txtAppKey.Text,
                    AppSecret = txtAppSecret.Text,
                    Password = txtPassword.Text,
                    PublicKey = txtAppPubKey.Text,
                    TenantName = txtTenantName.Text,
                    Username = txtUserName.Text
                };
                try
                {
                    KhtDAL dal = new KhtDAL(authBase, txtBaseUri.Text);
                    if (dal.DdyReady())
                    {

                        MessageBox.Show("配置搭搭云平台成功", "配置搭搭云平台成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show(dal.ErrMsg, "配置搭搭云平台失败", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message+"\r\n可能是用户名或密码错误","测试搭搭云平台失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    
                }
                
               
            }
            catch (Exception ex)
            {
                MyLog.WriteError("配置搭搭云平台", ex.Message + ex.StackTrace);
                MessageBox.Show(ex.Message+ex.StackTrace, "配置发生异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }

        private void FrmSettings_Load(object sender, EventArgs e)
        {
            if (File.Exists(settingFile))
            {
                
                cfg.Load(settingFile);               
                if (cfg.ContainsKey(SettingItems.ddyBaseUri)) txtBaseUri.Text = cfg.ReadString(SettingItems.ddyBaseUri);
                if (cfg.ContainsKey(SettingItems.ddyTenantName)) txtTenantName.Text = cfg.ReadString(SettingItems.ddyTenantName);
                if (cfg.ContainsKey(SettingItems.ddyUsername)) txtUserName.Text = cfg.ReadString(SettingItems.ddyUsername);
                if (cfg.ContainsKey(SettingItems.ddyPasswd)) txtPassword.Text = cfg.ReadString(SettingItems.ddyPasswd);
                if (cfg.ContainsKey(SettingItems.ddyAppKey)) txtAppKey.Text = cfg.ReadString(SettingItems.ddyAppKey);
                if (cfg.ContainsKey(SettingItems.ddySecret)) txtAppSecret.Text = cfg.ReadString(SettingItems.ddySecret);
                if (cfg.ContainsKey(SettingItems.ddyPubkey)) txtAppPubKey.Text = cfg.ReadString(SettingItems.ddyPubkey);
                if (cfg.ContainsKey(SettingItems.reportToEmail)) txtReportTo.Text = cfg.ReadString(SettingItems.reportToEmail);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            
            
            cfg.WriteString(SettingItems.ddyBaseUri, txtBaseUri.Text);
            cfg.WriteString(SettingItems.ddyTenantName, txtTenantName.Text);
            cfg.WriteString(SettingItems.ddyUsername, txtUserName.Text);
            cfg.WriteString(SettingItems.ddyPasswd, txtPassword.Text);
            cfg.WriteString(SettingItems.ddyAppKey, txtAppKey.Text);
            cfg.WriteString(SettingItems.ddySecret, txtAppSecret.Text);
            cfg.WriteString(SettingItems.ddyPubkey, txtAppPubKey.Text);
            cfg.WriteString(SettingItems.reportToEmail, txtReportTo.Text);
            cfg.Save(settingFile);
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
    internal class SettingItems
    {
       
        public static string ddyBaseUri = "DdyBaseUri";
        public static string ddyTenantName = "DdyTenantName";
        public static string ddyUsername = "DdyUsername";
        public static string ddyPasswd = "DdyPasswd";
        public static string ddyAppKey = "DdyAppKey";
        public static string ddySecret = "DdySecret";
        public static string ddyPubkey = "DdyPubkey";
        public static string reportToEmail = "ReportToEmail";
    }
}
